﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using SN.Services;
namespace SN.Report
{
    public partial class FormConnect : Form
    {
        public FormConnect()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新配置文件信息
        /// </summary>
        /// <param name="name">配置文件字段名称</param>
        /// <param name="Xvalue">值</param>
        private void UpdateConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //, "System.Data.SqlClient"
            // ConnectionStringSettings connSettings = new ConnectionStringSettings("connectionString", connectstr);
            ConnectionStringsSection connSection = config.ConnectionStrings;
            connSection.ConnectionStrings["connectionString"].ConnectionString = connectstr;
            //connSection.ConnectionStrings.Clear();
            //connSection.ConnectionStrings.Add(connSettings);
            config.Save(ConfigurationSaveMode.Modified);
            ////当前的配置文件更新成功。
            ConfigurationManager.RefreshSection("connectionString");// 刷新命名节，在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点
           // Application.Restart();//重新启动
        }
        public string connectstr;

        private void button1_Click(object sender, EventArgs e)
        {
            string[] ss = ConnectDialog.showSqlConnectionDialog();
            if (ss != null)
            {
                if (ss.Length > 0)
                {
                    this.textBox1.Text = ss[0];
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            connectstr = this.textBox1.Text.Trim();//值
            if (ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString != connectstr)
            {
                UpdateConfig();
            }
            DialogResult = DialogResult.OK;
        }

        private void FormConnect_Load(object sender, EventArgs e)
        {
            textBox1.Text = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        }

        private void FormConnect_Activated(object sender, EventArgs e)
        {
            button2.Focus();
        }
    }
}
